<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <SearchBarNew :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"
          style="width: 99.9%"
          row-key="id"
          :cell-style="cellStyle"
          @cell-click="cellClick"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" :min-width="column.minWidth" />

          <el-table-column label="操作" align="center" fixed="right" width="180">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.status == '1'||scope.row.status=='2'"
                v-hasPermi="['quality:productionCheck:check']"
                type="text"
                @click.stop="checkout(scope.row)"
              >检验</el-button>
              <span v-else class="el-button--text button_grey">检验</span>
              <el-button
                v-if="scope.row.canEdit == '1' ||scope.row.status == '0' && scope.row.automaticCreate !== '1'"
                v-hasPermi="['quality:productionCheck:edit']"
                type="text"
                @click.stop="edit(scope.row)"
              >编辑</el-button>
              <span v-else class="el-button--text button_grey">编辑</span>
              <el-button
                v-if="scope.row.status != '3' "
                v-hasPermi="['quality:productionCheck:delete']"
                type="text"
                @click.stop="deleteOne(scope.row)"
              >删除</el-button>
              <span v-else class="el-button--text button_grey">删除</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>

  </div>
</template>

<script>
import { list, remove, getInfo } from '@/api/qualityManage/productionInspectionForm'
import AddOrUpdate from './addOrUpdate'
import ShowDetailOld from '../comeMaterialCheckout/showQuality'
import { userList } from '@/api/system/dept'
import { list as materType } from '@/api/warehouseManagement/materialType'
import showOrUpdata from '@/views/qualityManage/comeMaterialCheckout/showOrUpdata'
export default {
  name: 'Index',
  data() {
    return {
      loading: false,
      pageId: '生产检验单', // 页面唯一标识
      pageExportId: '生产检验单导出', // 页面导出标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'code',
        isAsc: 'desc'
      },
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'statusName', label: '状态', canOrder: false, width: '100' },
        { prop: 'code', label: '生产检验单号', minWidth: '160' },
        { prop: 'checkByName', label: '检验人员', canOrder: false },
        { prop: 'checkDate', label: '检验时间', sortable: 'custom', minWidth: '180' },
        { prop: 'engineeringRequirementCode', label: '工艺要求', minWidth: '120' },
        { prop: 'engineeringProcessName', label: '工序', minWidth: '120' },
        { prop: 'qualityTemplateName', label: '检验模板', canOrder: false },
        { prop: 'checkNumber', label: '送检数量', canOrder: false },
        { prop: 'productTaskCode', label: '生产任务单号', minWidth: '160' },
        { prop: 'materialCode', label: '物料编码', minWidth: '160' },
        { prop: 'materialName', label: '物料名称', minWidth: '160', canOrder: false },
        { prop: 'materialModel', label: '规格型号', canOrder: false },
        { prop: 'materialTypeName', label: '物料分类', canOrder: false },
        { prop: 'materialUnitName', label: '物料单位', canOrder: false }

      ],
      defaultExport: [
      ],
      exportTableColumns: [// 表格常规字段（初始值）
        { prop: 'statusName', label: '状态', canOrder: false, width: '100' },
        { prop: 'code', label: '生产检验单号', minWidth: '200' },
        { prop: 'checkByName', label: '检验人员', canOrder: false },
        { prop: 'checkDate', label: '检验时间' },
        { prop: 'engineeringRequirementCode', label: '工艺要求', minWidth: '100' },
        { prop: 'engineeringProcessName', label: '工序', minWidth: '100' },
        { prop: 'qualityTemplateName', label: '检验模板', canOrder: false },
        { prop: 'checkNumber', label: '送检数量', canOrder: false, minWidth: this.SETTINGS.settings_decimalDigits_length },
        { prop: 'productTaskCode', label: '生产任务单号' },
        { prop: 'materialCode', label: '物料编码', minWidth: '200' },
        { prop: 'materialName', label: '物料名称', minWidth: '200', canOrder: false },
        { prop: 'materialModel', label: '规格型号', canOrder: false },
        { prop: 'materialTypeName', label: '物料分类', canOrder: false },
        { prop: 'materialUnitName', label: '物料单位', canOrder: false }

      ],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'allSearchValue', label: '', type: 'input' },
        { prop: 'checkBy', label: '检验人员', type: 'select', optionsName: 'userList' },
        { prop: 'materialType', label: '物料分类', type: 'select', optionsName: 'materialTypeList' },
        // { prop: 'materialCode', label: '物料编码', type: 'input' },
        // { prop: 'materialName', label: '物料名称', type: 'input' },
        // { prop: 'materialModel', label: '规格型号', type: 'input' },
        // { prop: 'materialTypeName', label: '物料分类', type: 'input' },
        // { prop: 'materialUnitName', label: '物料单位', type: 'input' },
        // { prop: 'code', label: '生产检验单号', type: 'input' },
        // { prop: 'productTaskCode', label: '生产任务单号', type: 'input' },
        // { prop: 'qualityTemplateName', label: '检验模板', type: 'input' },
        // { prop: 'checkByName', label: '检验人员', type: 'input' },
        { prop: 'statusList', label: '状态', type: 'select-multiple', optionsName: 'statusOptions2' },
        // { prop: 'checkNumber', label: '送检数量', type: 'input' },
        { prop: 'checkDate', label: '检验时间', type: 'time', isTime: true }

      ],
      userList: [],
      materialTypeList: [],
      statusOptions2: [ // 选择组件数据源
        // { value: undefined, label: '全部' },
        { value: '0', label: '草稿' },
        { value: '1', label: '待检验' },
        { value: '2', label: '检验中' },
        { value: '3', label: '已检验' }
      ],
      operationData: [ // 右上角的操作按钮
        { label: '新增', perm: 'quality:productionCheck:add' },
        { label: '批量删除', perm: 'quality:productionCheck:delete' },
        { label: '批量导出3', perm: 'quality:productionCheck:export' },
        { label: '页面配置', perm: '' }
      ],
      showKey: {
        type: 'column_two',
        keys: [
          { type: 'form', name: '生产检验单号', key: 'code' },
          { type: 'form', name: '检验人员', key: 'checkByName' },
          { type: 'form', name: '检验时间', key: 'checkDate' },
          { type: 'form', name: '物料编码', key: 'materialCode' },
          { type: 'form', name: '物料名称', key: 'materialName' },
          { type: 'form', name: '规格型号', key: 'materialModel' },
          { type: 'form', name: '物料单位', key: 'materialUnitName' },
          { type: 'form', name: '关联任务单', key: 'productTaskCode' },
          { type: 'form', name: '工艺要求', key: 'engineeringRequirementCode' },
          { type: 'form', name: '工序', key: 'engineeringProcessName' },
          { type: 'form', name: '检验模板', key: 'qualityTemplateName' },
          { type: 'form', name: '送检数量', key: 'checkNumber' },
          { type: 'form', name: '抽检数量', key: 'spotCheckNumber' },
          { type: 'form', name: '不合格数量', key: 'disqualificationNumber' },
          { type: 'form', name: '合格数量', key: 'qualifiedNumber' },
          { type: 'form', name: '判定结果', key: 'resultCode' },
          { type: 'isInForm', name: '是否入库', key: 'isInForm' },
          { type: 'file', name: '附件', fileName: 'fileName', fileRealName: 'fileRealName', fileUrl: 'url' },
          { type: 'form', name: '不良描述', key: 'description', length: 'double' },
          { type: 'file', name: '检验图例', fileName: 'templateFileNames', fileRealName: 'templateFileRealNames', fileUrl: 'templateFileUrls' },
          {
            type: 'table1', name: '整体项目', key: 'data1', columns: [ // type: 'table' 表示是表格
              { name: '检验项目', key: 'qualityItemName', minWidth: '120' }
            ]
          },
          {
            type: 'table', name: '个体项目', key: 'data', columns: [ // type: 'table' 表示是表格
              { name: '检验项目', key: 'qualityItemName', minWidth: '120' }
            ]
          }
        ]
      },
      showKey2: {
        type: 'column_two',
        keys: [
          { type: 'form', name: '生产检验单号', key: 'code' },
          { type: 'form', name: '检验人员', key: 'checkByName' },
          { type: 'form', name: '检验时间', key: 'checkDate' },
          { type: 'form', name: '物料编码', key: 'materialCode' },
          { type: 'form', name: '物料名称', key: 'materialName' },
          { type: 'form', name: '规格型号', key: 'materialModel' },
          { type: 'form', name: '物料单位', key: 'materialUnitName' },
          { type: 'form', name: '关联任务单', key: 'productTaskCode' },
          { type: 'form', name: '工艺要求', key: 'engineeringRequirementCode' },
          { type: 'form', name: '工序', key: 'engineeringProcessName' },
          { type: 'form', name: '检验模板', key: 'qualityTemplateName' },
          { type: 'form', name: '送检数量', key: 'checkNumber' },
          { type: 'form', name: '抽检数量', key: 'spotCheckNumber' },
          { type: 'form', name: '不合格数量', key: 'disqualificationNumber' },
          { type: 'form', name: '合格数量', key: 'qualifiedNumber' },
          { type: 'isInForm', name: '判定结果', key: 'resultCode' },
          { type: 'isInForm1', name: '是否入库', key: 'isInForm' },
          { type: 'file', name: '附件', fileName: 'fileName', fileRealName: 'fileRealName', fileUrl: 'url' },
          { type: 'remark', name: '不良描述', key: 'description', length: 'double' },
          { type: 'file1', name: '检验图例', fileName: 'templateFileNames', fileRealName: 'templateFileRealNames', fileUrl: 'templateFileUrls' },
          {
            type: 'table1', name: '整体项目', key: 'data1', columns: [ // type: 'table' 表示是表格
              { name: '检验项目', key: 'qualityItemName', minWidth: '120' }
            ]
          },
          {
            type: 'table', name: '个体项目', key: 'data', columns: [ // type: 'table' 表示是表格
              { name: '检验项目', key: 'qualityItemName', minWidth: '120' }
            ]
          }
        ]
      }
    }
  },
  mounted() {
    // this.$nextTick(() => {
    //   this.tableHeight = this.$refs.table_container.offsetHeight
    // })
    this.getUser()
    this.getTypeList()
  },
  methods: {
    getTypeList() {
      materType().then(res => {
        this.materialTypeList = res.data.map(item => {
          return { value: item.id, label: item.name }
        })
      })
    },
    getUser() {
      userList().then(res => {
        this.userList = res.data.map(item => {
          return { value: item.userId, label: item.realName }
        })
      })
    },
    cellStyle({ row, column }) {
      if (column.property == 'code') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
    },
    cellClick(row, column, cell) {
      switch (column.property) {
        case 'code':
          this.examine(row)
          break
      }
    },
    getList() {
      list(this.pageData).then(res => {
        res.data.map((item) => {
          if (item.status == 3) {
            item.canDelete = 0
          }
        })
        this.tableData = res.data
        this.total = res.total
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    refresh() {
      this.pageData.pageNum = 1
      this.getList()
    },
    add() {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
          }// props
        },
        area: ['1320px', '700px'],
        title: '新增生产检验单', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    edit(row) {
      if (row.canEdit == '1') {
        getInfo(row[this.mainId]).then(res => {
          // res.data.forEach(item => {
          if (res.data.resultCode == '通过') {
            res.data.resultCode = 0
          } else {
            res.data.resultCode = 1
          }
          if (res.data.isInForm == '入库') {
            res.data.isInForm = '是'
          } else {
            res.data.isInForm = '否'
          }
          // })
          const showData = this.$deepClone(res.data)
          const newlistData = JSON.parse(res.data.titleJson)
          const newlistData1 = JSON.parse(res.data.entityTitleJson)
          const newData = JSON.parse(res.data.qualityJson)
          const newData1 = JSON.parse(res.data.entityQualityJson)
          showData.listData = newlistData
          showData.listData1 = newlistData1
          showData.data = newData
          showData.data1 = newData1
          this.$layer.iframe({// 弹框
            content: {
              content: showOrUpdata, // 传递的组件对象
              parent: this, // 当前的vue对象
              data: {
                showData: showData,
                showKey: this.showKey2,
                pageId: this.pageId
              }// props
            },
            area: ['1320px', '700px'],
            title: '编辑生产检验单', // 标题
            shadeClose: false // 点击遮罩是否关闭
          })
        })
      } else {
        this.$layer.iframe({// 弹框
          content: {
            content: AddOrUpdate, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              id: row[this.mainId]
            }// props
          },
          area: ['1320px', '700px'],
          title: '编辑生产检验单', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      }
    },
    checkout(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row[this.mainId],
            show: true
          }// props
        },
        area: ['1320px', '700px'],
        title: '执行生产检验单', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    examine(row) {
      getInfo(row[this.mainId]).then(res => {
        if (res.data.resultCode == '' || res.data.resultCode == 'null') {
          res.data.resultCode = ''
        } else if (res.data.resultCode == 0) {
          res.data.resultCode = '通过'
        } else if (res.data.resultCode == 1) {
          res.data.resultCode = '不通过'
        }
        const showData = this.$deepClone(res.data)
        const newlistData = JSON.parse(res.data.titleJson)
        const newlistData1 = JSON.parse(res.data.entityTitleJson)
        const newData = JSON.parse(res.data.qualityJson)
        const newData1 = JSON.parse(res.data.entityQualityJson)
        showData.listData = newlistData
        showData.listData1 = newlistData1
        showData.data = newData
        showData.data1 = newData1
        this.$layer.iframe({// 弹框
          content: {
            content: ShowDetailOld, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              showData: showData,
              showKey: this.showKey
            }// props
          },
          area: ['1320px', '700px'],
          title: '查看生产检验单', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      })
    },
    deleteOne(row) {
      const _this = this
      this.$confirm('确定要删除吗', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', cancelButtonClass: 'cancelConfirm',
        type: 'warning'
      }).then(function() {
        remove(row[_this.mainId]).then(res => {
          _this.$message({
            message: res.msg,
            type: 'success'
          })
          _this.getList()
        })
      }).catch(() => {})
    },
    getExportData(data) {
      console.log(data)
      return list(data)
    },
    // 当前页的删除接口
    deleteMethod(deleteIds) {
      return remove(deleteIds)
    }
  }
}
</script>

<style lang="less" scoped>
</style>

